<template>
  <a-drawer
    :wrapClassName="`layout-deploy-drawer layout-deploy-drawer--${drawerClass}`"
    :visible="visible"
    :mask="false"
    :closable="false"
    placement="right"
    width="100%"
    :getContainer="false"
    :wrap-style="{ position: 'absolute' }"
    :destroyOnClose="true"
  >
    <template #title v-if="title || $slots.option">
      <span>{{ title }}</span>
      <div class="layout-deploy-drawer--option">
        <slot name="option"></slot>
      </div>
    </template>
    <div class="drawer-layout-main">
      <a-card v-if="$slots.form" :title="promptTitle" :bordered="false" class="drawer-layout-info">
        <slot name="form" />
      </a-card>
      <!-- 兼容非卡片页面 -->
      <div class="drawer-layout-other" v-if="$slots.other">
        <slot name="other" />
      </div>
      <a-card
        v-if="!$slots.other && tabList.length >= 0"
        :bordered="false"
        :tab-list="tabList"
        :defaultActiveTabKey="defActive"
        @tabChange="handelTabChange"
      >
        <div class="drawer-layout-cnt">
          <div class="drawer-layout-card">
            <slot></slot>
          </div>
        </div>
      </a-card>
    </div>
  </a-drawer>
</template>

<script>
export default {
  name: 'DrawerLayout',
  props: {
    title: {
      type: String
    },
    promptTitle: {
      type: String
    },
    tabList: {
      type: Array,
      default() {
        return []
      }
    },
    visible: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      drawerVisible: true,
      drawerClass: ''
    }
  },
  computed: {
    // 获取store中的menuCollapsed状态
    menuCollapsed() {
      return this.$store.state.menu.menuCollapsed
    },

    defActive() {
      if (this.tabList.length > 0) {
        return this.tabList[0].key || this.tabList[0].id || ''
      } else {
        return ''
      }
    }
  },
  watch: {
    menuCollapsed() {
      this.$nextTick(() => {
        this.drawerClass = this.menuCollapsed
      })
    }
  },
  methods: {
    handelTabChange(tab) {
      this.$emit('tabChange', tab)
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.drawerClass = this.menuCollapsed
    })
  }
}
</script>

<style lang="less">
.layout-deploy-drawer {
  z-index: 64 !important;

  &--option {
    position: absolute;
    right: 46px;
    top: 0;
    line-height: 52px;

    .ant-btn {
      margin-left: 12px;
      min-width: 110px;
    }
  }

  &.ant-drawer {
    transition: transform 0.3s;
  }

  &.ant-drawer-open {
    width: 100% !important;
  }

  .ant-drawer-content-wrapper {
    position: relative;
    // padding-top: 50px + 52;
    // padding-left: @siderWidth;
    transition: all 0.2s;

    .ant-drawer-content {
      background-color: @colorBg;
    }

    .ant-drawer-header {
      border-bottom-color: @colorBg;
      position: absolute;
      width: 100%;
      height: 56px;
      padding: 16px 46px;
      top: 0;
      z-index: 9;
    }

    .ant-drawer-body {
      padding: 12px;
    }

    .ant-drawer-header ~ .ant-drawer-body {
      padding-top: 68px;
    }
  }

  &--true {
    .ant-drawer-content-wrapper {
      // padding-left: @siderCollapWidth;
      transition: all 0.2s;
    }
  }
}

.drawer-layout-main {
  .drawer-layout-card {
    min-height: 220px;
    width: 100%;
  }

  .ant-card {
    & + .ant-card {
      margin-top: 12px;
    }

    .ant-card-head-title {
      padding: 0;
      line-height: 48px;
      font-size: 14px;
      font-weight: normal;
    }

    .ant-tabs-bar {
      border: none;
    }

    .ant-tabs-tab {
      padding: 14px 16px;
      font-size: 14px;
    }
  }

  .drawer-layout-info {
    margin-bottom: 12px;
    .ant-form {
      display: flex;
      justify-content: space-between;
      flex-direction: row;
      flex-wrap: wrap;
      .ant-form-item {
        width: 25%;
        padding-right: 30px;

        .ant-input-number {
          width: 100%;
        }
      }
    }
  }

  .drawer-layout-other {
    position: relative;
    width: 100%;
    min-height: 400px;
  }

  .drawer-layout--title {
    position: relative;
    font-size: 16px;
    color: #02437e;
    background-color: #f4f7f9;
    padding-left: 20px;
    line-height: 36px;
    margin: 14px 0;

    &::before {
      content: '';
      width: 6px;
      height: 16px;
      background-color: @primary;
      position: absolute;
      left: 8px;
      top: 10px;
    }

    &__btn {
      float: right;
      line-height: 1;
    }
  }

  .drawer-layout--sub {
    padding-left: 20px;
    position: relative;
    margin: 12px 0;

    &::before {
      content: '';
      position: absolute;
      height: 4px;
      width: 4px;
      background-color: #3f7bff;
      left: 8px;
      top: 10px;
    }
  }
}
</style>
